package com.yun.utils;

import java.util.HashMap;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

import com.alibaba.fastjson.JSONObject;

/**
 * 
*项目名称：企智云_信发
*创建作者：瞿黑子
*创建日期：2019年2月13日
*详细描述：极光推送
 */
public class JiGunag {

   /* public static void main(String[] args) {
        String[] machine = { "0xbe1ff4b1_0xbe1bf4d1" };
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", "content");
        map.put("type", "appUrl");
        String result = JSONObject.toJSONString(map); 
        pushMessage2(machine, result, "");
    }*/
    

  
  /*  private static final String appKey = "0de8cf9794e26720d65dadcc";
    private static final String masterSecret = "01bdb15a784f7d896ed52278";*/


    private static final String appKey = "c94bde253ffc1107a523e3c4";
    private static final   String masterSecret = "a4efd0fbacee6d857103e284";
    private static JPushClient jpushClient = null;

    private static synchronized JPushClient getJPushClient() {
        if (jpushClient == null) {
            jpushClient = new JPushClient(masterSecret, appKey);
        }
        return jpushClient;
    }
    
    
    public static void pushMessage(String[] machine, String text) {
        try {
            getJPushClient().sendPush(pushCustomMessage(machine, text, null));
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
     //带通知栏的推送
   public static void push(String [] machieid,String text) {
      
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android())
//              .setAudience(Audience.all())
                .setAudience(Audience.tag(machieid))
                .setNotification(Notification.android(text, "文件夹",null))
                .setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
        try {
            @SuppressWarnings("unused")
            PushResult pu = jpushClient.sendPush(payload);
        } catch (Exception e) {
            e.printStackTrace();
        }
   }
    
    public static void main(String[] args) {
      /*	String [] machieid= {"abc123456"};
           pushMessage(machieid,"123");*/

    }
    
    public static void pushMessage2(String[] machine, String text, String title) {
        try {
            PushResult result =  getJPushClient().sendPush(pushCustomMessage(machine, text, title));
            System.out.println(result.toString());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
   //心跳控制
    public static void pushMessage3(String json) {
        try {
            getJPushClient().sendPush(pushCustomMessage2(json, "200"));
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    /**
     * t
     * 
     * @return
     */
    public static PushPayload pushCustomMessage(String[] machine, String text, String title) {
        return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(machine))
                .setMessage(Message.newBuilder().setTitle(title).setMsgContent(text).build())
                .setOptions(Options.newBuilder()
                        // 此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意义
                        .setApnsProduction(false)
                        // 此字段是给开发者自己给推送编号，方便推送者分辨推送记录
                        .setSendno(1)
                        // 此字段的值是用来指定本推送的离线保存时长，如果不传此字段则默认保存一天，最多指定保留十天，单位为秒
                        .setTimeToLive(0).build())
                .build();
    }

    /**
     * 
     * @return
     */
    public static PushPayload pushCustomMessage2(String text, String title) {
        return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
                .setMessage(Message.newBuilder().setTitle(title).setMsgContent(text).build())
                .setOptions(Options.newBuilder()
                        // 此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意义
                        .setApnsProduction(false)
                        // 此字段是给开发者自己给推送编号，方便推送者分辨推送记录
                        .setSendno(1)
                        // 此字段的值是用来指定本推送的离线保存时长，如果不传此字段则默认保存一天，最多指定保留十天，单位为秒
                        .setTimeToLive(0).build())
                .build();
    }
    
    /**
     * 推送自定义消息
     * @return
     */
    public static PushPayload pushCustomMessageTest(String text,String title) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                //.setAudience(Audience.tag(new String[]{"0xbe1ff4b1_0xbe1bf4d1"}))
                .setAudience(Audience.all())
                .setMessage(Message.newBuilder()
                        .setTitle(title)
                        .setMsgContent(text)
                        .build())
                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境，false表示开发，true表示生产；对android和自定义消息无意义
                        .setApnsProduction(false)
                        //此字段是给开发者自己给推送编号，方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长，如果不传此字段则默认保存一天，最多指定保留十天，单位为秒
                        .setTimeToLive(0)
                        .build()
                )
                .build();
    }
    
    
    public static void pushMessageTest(String text,String title) {
        try {
            getJPushClient().sendPush(pushCustomMessageTest(text,title));
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }
    
    

}
